package com.hanbang.maige.view.quanzi.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.hanbang.maige.R;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.code.base.view.fragment.BaseListFragment;
import com.hanbang.maige.code.broadcast.BroadcastUtils;
import com.hanbang.maige.mode.javabean.quanzi.QuanZiData;
import com.hanbang.maige.pressenter.quanzi.QuanZiPresenter;
import com.hanbang.maige.utils.ui.divider.HorizontalDividerItemDecoration;
import com.hanbang.maige.view.quanzi.IQuanZiBaseView;
import com.hanbang.maige.view.quanzi.activity.FaBuFriendActivity;
import com.hanbang.maige.view.quanzi.adapter.QuanZiAdapter;
import com.hanbang.maige.widget.empty_layout.ContextData;

import java.util.List;

/**
 * 作者　　: 李坤
 * 创建时间:2016/10/17　15:45
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：
 */

public class QuanZiFragment extends BaseListFragment<IQuanZiBaseView.IQuanZiView, QuanZiPresenter, QuanZiData>
        implements IQuanZiBaseView.IQuanZiView, QuanZiAdapter.OnButtnClickListener {

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                presenter.getHttpData(true);
            }
        }, new IntentFilter(BroadcastUtils.QUANZI_PUBLISH));

        presenter.getHttpData(true);
    }

    @Override
    public QuanZiPresenter initPressenter() {
        return new QuanZiPresenter();
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_quanzi;
    }

    @Override
    public void initView() {
        super.initView();
        toolbar.setStatusViewHeight();
        toolbar.setTitle("圈子");
        toolbar.addAction(0, "发布", R.drawable.material_xiangji);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                FaBuFriendActivity.startUI(getContext());
                return true;
            }
        });
    }

    @Override
    public CommonAdapter getAdapter() {
        return adapter = new QuanZiAdapter(getContext(), listDatas, this);
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return new HorizontalDividerItemDecoration.Builder(getContext())
                .colorResId(R.color.gray_ee)
                .sizeResId(R.dimen.dp_10)
                .build();
    }


    @Override
    public void onDianZhan(int position, int quanziId) {
        if (!listDatas.get(position).isdianzan()) {
            presenter.dianzhan(quanziId, position);
        } else {
            showInfoSnackbar("您已经点赞");
        }
    }

    @Override
    public void updataUI(List<QuanZiData> validData) {

        listDatas.addAll(validData);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void updataUI(Integer position) {

        listDatas.get(position).setIsdianzan(true);
        listDatas.get(position).addUserDianZhan();
        adapter.notifyItemChanged(position);
    }

    @Override
    public void onRefresh() {
        presenter.getHttpData(true);
    }

    @Override
    public void onLoadding() {
        presenter.getHttpData(false);
    }

    @Override
    public void onRetryClick(ContextData data) {
        presenter.getHttpData(true);
    }

    @Override
    public void onEmptyClick(ContextData data) {
        presenter.getHttpData(true);
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, QuanZiData data, int position) {

    }


}
